<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\string\StringUtils;

class SinglepageAction extends CommonAction {
	private $singleCate = null;
	protected $serviceBean="information.informationSinglepage.service";

	public function C_start()
	{
		parent::C_start();
		$this->singleCate = Beans::get("information.informationSinglepageCate.service");
	}

	public function add(HttpRequest $request) {
	
		parent::add($request);
		$this->assign('admin_name',adminName());
		$this->assign("singleCate", $this->singleCate->getItems());
        $this->assign("title", "添加单页");
        $this->assign('bread', array('信息管理','单页列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/singlepage/index'),'javascript:;'));
	
	}

	public function edit(HttpRequest $request) {
		parent::edit($request);
		$this->assign('admin_name',adminName());
		$this->assign("singleCate", $this->singleCate->getItems());
		$this->assign("title", "编辑单页");
        $this->assign('bread', array('信息管理','单页列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/singlepage/index'),'javascript:;'));
	}
	
	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','单页表');
		$s_title = $request->getParameter('s_title',"urldecode");
		$this->assign('s_title',$s_title);
		if($s_title !== '' && $s_title!== NULL)$conditions['title'] = '%'.$s_title.'%';

		$s_cate_id = $request->getParameter('s_cate_id',"intval");
		$this->assign('s_cate_id',$s_cate_id);
		if($s_cate_id !=0)$conditions['cate_id'] = "$s_cate_id";

		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);
		
		if($s_s_addtime !== '' && $s_s_addtime!== NULL){ 
			$conditions['addtime'] = array(); 
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		} 
								
		
		if($s_e_addtime !== '' && $s_s_addtime!== NULL){
			if(!$conditions['addtime']){ $conditions['addtime'] = array();}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}
								
		$s_manager_id = $request->getParameter('s_manager_id');
		$this->assign('s_manager_id',$s_manager_id);
		if($s_manager_id !== '' && $s_manager_id!== NULL)$conditions['manager_id'] = '%'.$s_manager_id.'%';
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->setConditions($conditions);

		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		//parent::index($request);

		$service = Beans::get($this->getServiceBean());
        $total = $service->count($this->getConditions());
        $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
            array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
        $this->assign('items', $items);

        if( $request -> getParameter('export') == 1 ){
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this -> setConditions($conditions);
            }
            $this->setPage(1);
            $this->setPagesize(2147483648);
            $items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
                array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["title"];
                $tmp[] = $v["information_singlepage_cate_name"];
                $tmp[] = $v["addtime"];
                $tmp[] = $v["admin_manager_username"];
                $tmp[] = $v["sort_index"];
                $excel[] = $tmp;
            }
            //array('title','image_url','content','information_singlepage_cate_id','summary','addtime','admin_manager_id','app_id')
            $header = array('标题','类型','添加时间','操作人','排序');
            phpexcel("单页表列表",$header,$excel);
            exit();
        }

        $this->generatePageMenu($total);
	
		/**--tmpl-indexafter--**/
		$this->assign("singleCate", $this->singleCate->getItems());
		$this->assign("cate_url", "/admin/singlepageCate/index");
		$this->assign("title", "单页列表");
        $this->assign('bread', array('信息管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
        $this->setView(strtolower($request->getAction()) . '/index');
	}
	
	/**-- doAddMethod --**/
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		$data['addtime'] = date("Y-m-d H:i:s",time());
		$data['is_full'] = $data['is_full']=="on"?1:0;
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	
	/**-- doEditMethod --**/
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		$data['is_full'] = $data['is_full']=="on"?1:0;
		/**--tmpl-todo--**/
		//如果图片输入框是空，表示没有重新上传图片，则图片路径不变
		if($data['image_url']==""){
			$data['image_url']== $data['old_image'];
		}else{
			//如果上传了新的图片，则将旧的图片删除
			del_file(APP_ROOT.$data['old_image']);
		}
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}

	public function delete( HttpRequest $request ) {
		$id = $request->getParameter('id');
		if ( $id == "" ) AjaxResult::ajaxResult('error', COM_ERR_MSG);
		$service = Beans::get($this->getServiceBean());

		$item = $service->getItem(array('id'=>$id));//为了获取对应封面图片的路径
		del_file(APP_ROOT.$item["image_url"]);//删除对应图片
		if ( $service->delete($id) ) {
			AjaxResult::ajaxSuccessResult();
		} else {
			AjaxResult::ajaxFailtureResult();
		}
	}

	public function del_img(HttpRequest $request){
		$image_url = $request->getParameter('image_url');
		$full_url = APP_ROOT.$image_url;
		$result = del_file($full_url);
		if($result==0)
		{
			AjaxResult::ajaxSuccessResult();
		}
		else
		{
			AjaxResult::ajaxFailtureResult();
		}
		die();
	}

}
